package com.jerry.base.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.Scanner;

/**
 * User: Jerry
 * Date: 2024/2/24
 * Time: 11:27
 * Description:
 */
public class TwoThread {

    public static void main(String[] args) {
        A a = new A();
        B b = new B(a);

        a.start();
        b.start();
    }

}

@Slf4j
class A extends Thread {

    @Override
    public void run() {
        while (true) {
            Random random = new Random();
            int number = random.nextInt(100);
            System.out.println(number);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

@Slf4j
class B extends Thread {

    Scanner scanner = new Scanner(System.in);

    private A a;

    B(A a) {
        this.a = a;
        a.setDaemon(true);
    }

    @Override
    public void run() {
        while (true) {
            System.out.println("请输入字母：");
            String letter = scanner.next();
            if ("Q".equals(letter)) {
                break;
            }
        }
        log.info("B线程结束");
    }
}
